﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Ylh.DAL;

namespace Ylh.Data
{
  public  class RefUpdate
    {
        public static int Update(Type T, object O, string ConStr)
        {
            TableAttribute TableName;
            ColumnAttribute[] Columns;
            EntityBasePub.GetEntityProperty(T, out TableName, out  Columns);

            EntityBase baseO = (EntityBase)O;
            object ColumnValue = String.Empty;
            Dictionary<string, object> PreWhere = new Dictionary<string, object>();
            int i = 0;
            if (baseO.PropertyChangeList.Count == 0) return 0;

            String StrSet = String.Empty;
            String StrWhere = String.Empty;
            foreach (ColumnAttribute item in Columns)
            {
                ColumnValue = T.GetProperty(item.ColumnName).GetValue(O, null);
                if (item.IsPrimaryKey && ColumnValue == null)
                    throw new Exception("主键没有赋值,不能执行更新语句!");

                if (item.IsPrimaryKey && ColumnValue != null)
                {
                    PreWhere.Add("@" + item.ColumnName + i, ColumnValue);
                    StrWhere += " [" + item.ColumnName + "] = " + "@" + item.ColumnName + i + ",";
                    i++;
                }

                if (!item.IsPrimaryKey && baseO.PropertyChangeList.Contains(item.ColumnName))
                {
                    PreWhere.Add("@" + item.ColumnName + i, ColumnValue);
                    StrSet += " [" + item.ColumnName + "] = " + "@" + item.ColumnName + i + ",";
                    i++;
                }
            }
            String UpdateCmd = @" Update " + TableName.TableName + " SET " + StrSet.TrimEnd(',') + "  WHERE " + StrWhere.TrimEnd(',');
            return DbFactory.CreateInstance().CreateDb().ExecuteNonQuery(UpdateCmd, CommandType.Text, PreWhere, ConStr);
        }


        public static int Update(Type T, DataTable dt, string ConStr)
        {
            TableAttribute TableName;
            ColumnAttribute[] Columns;
            EntityBasePub.GetEntityProperty(T, out TableName, out  Columns);

            List<string> PrimaryKeyList = new List<string>();
            List<String> UpdateColumn = new List<string>();
            String SelectColumn = String.Empty;
            foreach (ColumnAttribute Col in Columns)
            {

                if (Col.IsPrimaryKey) PrimaryKeyList.Add(Col.ColumnName);

                if (Col.Identity) continue;

                UpdateColumn.Add(Col.ColumnName);

                SelectColumn += Col.ColumnName + ",";
            }
            String StrSelectCmd = @"SELECT TOP 0 " + SelectColumn.TrimEnd(',') + " FROM " + TableName.TableName;

            return DbFactory.CreateInstance().CreateDb().UpdateDataTable(StrSelectCmd, dt, TableName.TableName, PrimaryKeyList, UpdateColumn, ConStr);
        }
    }
}
